﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CommonMvc.Models;
using CommonMvc.Models.CommonModels;
using CommonMvc.Models.Concreate;
using Newtonsoft.Json;
using Microsoft.EntityFrameworkCore;
using CommonMvc.Common;
using CommonMvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using Microsoft.Extensions.Caching.Distributed;
using CommonMvc.Helper;
using System.IO;
using MimeKit;

namespace CommonMvc.Controllers
{

    public class PersonController : Controller
    {
        DataContext _dataBaseContext;
        ICookieManager _cookieManager;
        IWorkContext _workContext;
        ILogger _logger = LogManager.GetLogger("console");
        private readonly IDistributedCache _distributedCache;



        public PersonController(DataContext context, ICookieManager cookieManager, IWorkContext workContext , IDistributedCache distributedCache) //依赖注入得到实例  
        {
            _workContext = workContext;
            _dataBaseContext = context;
            _cookieManager = cookieManager;
            _distributedCache = distributedCache;
        
       
        }
    
        [TypeFilter(typeof(BaseAuthorizationFilter),Arguments = new object[] { false,"123|456"})]
        public IActionResult Create()
        {
            _cookieManager.SetCookie("cookie1", "hahacookie");
            _logger.Info("你访问了首页");
            _logger.Warn("警告信息");
            _logger.Error(_distributedCache.GetString("dataString"));
            //_distributedCache.SetString("dataString", "dataStringvalue");
            //Debug.Write(_distributedCache.GetString("data"));
            return View();
        }
        [HttpPost]
        public IActionResult Create(Person item)
        {

            _dataBaseContext.Persons.Add(item);
            _dataBaseContext.SaveChanges();
            return Redirect("List");
        }

        public IActionResult List()
        {
            List<Person> list = null;
           
            list = _dataBaseContext.Persons.ToList();
           
            return View(list);
        }

        public IActionResult Test()
        {
             IEnumerable<Person> list = _dataBaseContext.Persons.ToList();

          
            foreach (Person person in list) {
                Debug.WriteLine("person:  " + person.Id.ToString());
                foreach (PersonRole pr in person.PersonRoles) {
                    Debug.WriteLine("personrole:  " + pr.RoleId.ToString());                   
                     Debug.WriteLine("rolename:  " + pr.Role.Name);                    
                }
              
            }
         
            return Content("cookie :" + _cookieManager.GetCookieValue("cookie1"));
        }



        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        protected override void Dispose(bool disposing)
        {
            _dataBaseContext.Dispose();
            base.Dispose(disposing);
        }


       
    }
}
